package com.kunchi.isb.base.api;

import com.google.common.collect.Maps;
import com.kunchi.isb.base.dto.ISBDto;
import com.kunchi.isb.base.result.ISBResult;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Map;
import java.util.Set;

public class ISBApi<T extends ISBDto,D> {

    private String host;
    private String port;
    private String serviceName;
    private String servicePath;


    public String getServiceName() {
        return serviceName;
    }

    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }

    public String getServicePath() {
        return servicePath;
    }

    public void setServicePath(String servicePath) {
        this.servicePath = servicePath;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    /**
     * 平铺
     * @param param
     * @return
     */
    public ISBResult<D> callPavedService(T param){
        String url ="http://"+this.getHost()+":"+this.getPort()+ this.getServiceName()+"/"+this.getServicePath()+ "/";
        RestTemplate restTemplate = new RestTemplate();
        url=findUri(beanToMap(param),url);
        ResponseEntity<ISBResult> responseEntity = restTemplate.postForEntity(url,param, ISBResult.class);
        return responseEntity.getBody();
    }

    /**
     * 对象结构
     * @param param
     * @return
     */
    public ISBResult<D> callStructedpService(T param){
        String url ="http://"+this.getHost()+":"+this.getPort()+ this.getServiceName()+"/"+this.getServicePath()+ "/";
        RestTemplate restTemplate = new RestTemplate();
        url=findUri(null,url);
        ResponseEntity<ISBResult> responseEntity = restTemplate.postForEntity(url,param, ISBResult.class);
        return responseEntity.getBody();
    }

    /**
     * 根据参数拼接url进行Get查询时使用
     * @param map
     * @param url
     * @return
     */
    private static String findUri(Map<String,Object> map, String url){
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        if(null!=map){
            Set<String> keys = map.keySet();
            for(String key:keys){
                builder.queryParam(key,map.get(key));
            }
        }
        return builder.toUriString();
    }

    /**
     * 将对象转化为Map
     * @param bean
     * @param <T>
     * @return
     */
    private static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                if(null==beanMap.get(key)){
                    continue;
                }
                map.put(key+"", beanMap.get(key));
            }
        }
        return map;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }
}
